package cn.edu.cqut.mdm.web;

import java.util.Collection;

import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.cqut.mdm.container.NetworkPublishedModuleContainer;
import cn.edu.cqut.mdm.core.module.RemoteModuleEntity;

@Controller
@RequestMapping("/")
public class PublishController {
	@RequestMapping("/modules")
	public String modulesDisplay(Model model) {
		model.addAttribute("modules",  NetworkPublishedModuleContainer.getInstance().getAllModuleMap().values());
		return "modules";
	}
	@RequestMapping("/command/modules")
	@ResponseBody
	public String modules() {
		Collection<RemoteModuleEntity> remoteModuleEntities = NetworkPublishedModuleContainer.getInstance().getAllModuleMap().values();
		JsonConfig config = new JsonConfig();
		config.setRootClass(RemoteModuleEntity.class);
		config.setIgnoreTransientFields(true);
		String result = JSONArray.fromObject(remoteModuleEntities, config).toString();
		return result;
	}

}